/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.managedbean.frontend;

import com.rbh.risk.entity.RiskData;
import com.rbh.risk.entity.SeverityLevel;
import com.rbh.risk.entity.Workgroup;
import com.rbh.risk.lazy.LazyRiskDataModel;
import com.rbh.risk.service.RiskDataService;
import com.rbh.risk.service.SeverityLevelService;
import com.rbh.risk.util.Constants;
import com.rbh.risk.util.DateUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author M
 */
@ManagedBean(name = "riskListBean")
@SessionScoped
public class RiskListBean implements Serializable {

    private static final long serialVersionUID = 1L;
    /*
     * Selected Object
     */
    @ManagedProperty(value = "#{selectedRisk}")
    private RiskData selectedRisk;
    @ManagedProperty(value = "#{selectedDateTimeFrom}")
    private Date selectedDateTimeFrom;
    @ManagedProperty(value = "#{selectedDateTimeTo}")
    private Date selectedDateTimeTo;
    /*
     * Object
     */
    @ManagedProperty(value = "#{risks}")
    private List<RiskData> risks;
    @ManagedProperty(value = "#{isMedError}")
    private boolean isMedError;
    /*
     * Object List
     */
    @ManagedProperty(value = "#{levelNameList}")
    private List<String> levelNameList;
    /* Injection Beans */
    @ManagedProperty(value = "#{privilegeBean}")
    private PrivilegeBean privilegeBean;
    @ManagedProperty(value = "#{riskAddBean}")
    private RiskAddBean riskAddBean;
    /*
     * Service
     */
    @ManagedProperty(value = "#{riskDataService}")
    private RiskDataService riskDataService;
    @ManagedProperty(value = "#{severityLevelService}")
    private SeverityLevelService severityLevelService;
    @ManagedProperty(value = "#{fromMinDate}")
    private Date fromMinDate;
    @ManagedProperty(value = "#{toMinDate}")
    private Date toMinDate;
    @ManagedProperty(value = "#{maxDate}")
    private Date maxDate;
    /*
     * Injection Field
     */
    @ManagedProperty(value = "#{directView}")
    private boolean directView;
    @ManagedProperty(value = "#{level}")
    private SeverityLevel level;
    @ManagedProperty(value = "#{workgroup}")
    private Workgroup workgroup;

    /* Lazy */
    @ManagedProperty(value = "#{lazyRiskDataModel}")
    private LazyDataModel<RiskData> lazyRiskDataModel;

    /**
     * Creates a new instance of riskAddBean
     */
    public RiskListBean() {
        lazyRiskDataModel = new LazyRiskDataModel();
    }

    @PostConstruct
    public void init() {
        isMedError = false;
        directView = false;
        workgroup = null;
        level = null;
        selectedRisk = null;
        risks = null;
        selectedDateTimeFrom = DateUtil.adjustDate(new Date(), 0, -1, 0);
        selectedDateTimeTo = DateUtil.adjustDate(new Date(), 0, 0, 0);
    }

    /*
     * Method For AutoComplete
     */
    public void handleDateSelectFrom(SelectEvent event) {
        selectedDateTimeFrom = (Date) event.getObject();
        String[] input = {DateUtil.convertDateToString(selectedDateTimeFrom), DateUtil.convertDateToString(selectedDateTimeTo)};
        ((LazyRiskDataModel)lazyRiskDataModel).setDateFilter(input);
    }

    public void handleDateSelectTo(SelectEvent event) {
        selectedDateTimeTo = (Date) event.getObject();
        String[] input = {DateUtil.convertDateToString(selectedDateTimeFrom), DateUtil.convertDateToString(selectedDateTimeTo)};
        ((LazyRiskDataModel)lazyRiskDataModel).setDateFilter(input);
    }

    public void delete() {
        try {
            riskDataService.delete(selectedRisk);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "ลบข้อมูลความเสี่ยงสำเร็จ"));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failure", ex.getMessage()));
        } finally {
            this.initfindRisk();
        }
    }

    public void review() {
        try {
            riskDataService.update(privilegeBean.setReviewLevel(selectedRisk));
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "ทบทวนข้อมูลความเสี่ยงสำเร็จ"));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failure", ex.getMessage()));
        } finally {
            this.initfindRisk();
        }
    }

    public void unReview() {
        try {
            riskDataService.update(privilegeBean.setUnReviewLevel(selectedRisk));
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "ยกเลิกทบทวนข้อมูลความเสี่ยงสำเร็จ"));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failure", ex.getMessage()));
        } finally {
            this.initfindRisk();
        }
    }

    public boolean cancelReview() {
        return privilegeBean.cancelReview(selectedRisk);
    }

    public boolean canReview() {
        return privilegeBean.canReview(selectedRisk);
    }

    public boolean canEdit() {
        return privilegeBean.canEditRisk(selectedRisk);
    }

    public boolean canDelete() {
        return privilegeBean.canDeleteRisk(selectedRisk);
    }

    public boolean canManage() {
        return privilegeBean.isWorkgroup(selectedRisk);
    }

    public void initfindRisk() {
        if (directView) {
            risks = riskDataService.getByProfile(privilegeBean.getHashMapACL(), selectedDateTimeFrom, selectedDateTimeTo);
        } else {
            risks = riskDataService.getListRiskDetail(privilegeBean.getHashMapACL(), workgroup, level, selectedDateTimeFrom, selectedDateTimeTo);
        }
    }

    public String injectedRiskData() throws Exception {
        return riskAddBean.injectedRiskData(selectedRisk);
    }

    public String reset() {
        this.init();
        directView = true;
//        this.initfindRisk();
        return Constants.PATH_REPORT;
    }

    public void onRowSelect(SelectEvent event) throws Exception {
        /* Reopened transaction */
        selectedRisk = riskDataService.getById(((RiskData) event.getObject()).getDataId());
        isMedError = (selectedRisk.getCategory().getCategoryId().intValue() != 3);
    }

    public RiskData getSelectedRisk() {
        return selectedRisk;
    }

    public void setSelectedRisk(RiskData selectedRisk) {
        this.selectedRisk = selectedRisk;
    }

    public RiskDataService getRiskDataService() {
        return riskDataService;
    }

    public void setRiskDataService(RiskDataService riskDataService) {
        this.riskDataService = riskDataService;
    }

    public List<RiskData> getRisks() {
        return risks;
    }

    public void setRisks(List<RiskData> risks) {
        this.risks = risks;
    }

    public Date getSelectedDateTimeFrom() {
        return selectedDateTimeFrom;
    }

    public void setSelectedDateTimeFrom(Date selectedDateTimeFrom) {
        this.selectedDateTimeFrom = selectedDateTimeFrom;
    }

    public Date getSelectedDateTimeTo() {
        return selectedDateTimeTo;
    }

    public void setSelectedDateTimeTo(Date selectedDateTimeTo) {
        this.selectedDateTimeTo = selectedDateTimeTo;
    }

    public Date getFromMinDate() {
        return DateUtil.adjustDate(selectedDateTimeFrom, 0, -12, 0);
    }

    public void setFromMinDate(Date fromMinDate) {
        this.fromMinDate = fromMinDate;
    }

    public Date getToMinDate() {
        return DateUtil.adjustDate(selectedDateTimeFrom, 0, 0, 0);
    }

    public void setToMinDate(Date toMinDate) {
        this.toMinDate = toMinDate;
    }

    public Date getMaxDate() {
        return DateUtil.resetTime(new Date());
    }

    public void setMaxDate(Date maxDate) {
        this.maxDate = maxDate;
    }

    public boolean isIsMedError() {
        return isMedError;
    }

    public void setIsMedError(boolean isMedError) {
        this.isMedError = isMedError;
    }

    public SeverityLevel getLevel() {
        return level;
    }

    public void setLevel(SeverityLevel level) {
        this.level = level;
    }

    public Workgroup getWorkgroup() {
        return workgroup;
    }

    public void setWorkgroup(Workgroup workgroup) {
        this.workgroup = workgroup;
    }

    public PrivilegeBean getPrivilegeBean() {
        return privilegeBean;
    }

    public void setPrivilegeBean(PrivilegeBean privilegeBean) {
        this.privilegeBean = privilegeBean;
    }

    public boolean isDirectView() {
        return directView;
    }

    public void setDirectView(boolean directView) {
        this.directView = directView;
    }

    public RiskAddBean getRiskAddBean() {
        return riskAddBean;
    }

    public void setRiskAddBean(RiskAddBean riskAddBean) {
        this.riskAddBean = riskAddBean;
    }

    public LazyDataModel<RiskData> getLazyRiskDataModel() {
        return lazyRiskDataModel;
    }

    public void setLazyRiskDataModel(LazyDataModel<RiskData> lazyRiskDataModel) {
        this.lazyRiskDataModel = lazyRiskDataModel;
    }

    public SeverityLevelService getSeverityLevelService() {
        return severityLevelService;
    }

    public void setSeverityLevelService(SeverityLevelService severityLevelService) {
        this.severityLevelService = severityLevelService;
    }

    public List<String> getLevelNameList() {
        List<String> results = new ArrayList<>();
        for (SeverityLevel rs : severityLevelService.getAll()) {
            results.add(rs.getSeverityLevelName());
        }
        return results;
    }

    public void setLevelNameList(List<String> levelNameList) {
        this.levelNameList = levelNameList;
    }
}